package demo1;

import org.junit.Test;

import java.util.*;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.*;

/**
 * @author lilingzhi
 * @since 2022/12/23 10:24
 */
//180Collectors | | 183
public class GroupByDemo {
    static List<Demo>  demos = new ArrayList<>();
    static {
        demos = Arrays.asList(new Demo().setName("ZS").setType(1), new Demo().setName("ZS").setType(2), new Demo().setName("jks").setType(3));
    }

    @Test
    public void test() {
        Map<String, Long> collect = demos.stream().collect(groupingBy(Demo::getName, counting()));
        Map<String, Optional<Demo>> collect1 = demos.stream().collect(groupingBy(Demo::getName, maxBy(comparingInt(Demo::getType))));
        for (Map.Entry<String, Optional<Demo>> entry : collect1.entrySet()) {
            System.out.println(entry.getValue());
        }

        Map<String, Optional<Optional<Demo>>> collect2 = demos.stream().collect(groupingBy(Demo::getName, collectingAndThen(maxBy(comparingInt(Demo::getType)), Optional::ofNullable)));
    }

}
